package model;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

import network.TCPServer;
import network.UDPRecordAndDispatch;

import controller.Outils;


public class Model {

	private List<Call> allCall;
	private Boolean showDialer;
	private SoundSetting defaultSoundSetting;
	private ConcurrentLinkedQueue<Integer> fifo;
	private boolean ShowSoundSetting;
	private Properties properties;
	private Call callSelected;

	public Model(){
		allCall = new ArrayList<Call>();
		showDialer=false;
		ShowSoundSetting=false;
		defaultSoundSetting = new SoundSetting();
		fifo = new ConcurrentLinkedQueue<Integer>();
		initProperties();
		initFifo();
		



	}

	private void initProperties() {		
		//file properties is loaded once at program startup
		//during all the program life, to get a properties, use model.getProperties().getProperty("firstname") for example
			try {
				properties = Outils.load(getClass().getResource("/config.properties"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	private void initFifo() {
		

		int startUDP = Integer.parseInt(this.properties.getProperty("udp_port_min"));
		int endUDP = Integer.parseInt(this.properties.getProperty("udp_port_max"));
		System.out.println("[init FIFO] "+startUDP+"/"+endUDP);
		for(int i = startUDP; i<=endUDP;i++){
			fifo.add(i);
		}
		System.out.println("[init FIFO] Connexion max = "+fifo.size());
	}

	public void addCall(Call call){
		this.allCall.add(call);
	}
	
	public void removeCall(Call call){
		this.allCall.remove(call);
	}
	

		public List<Call> getCall(EnumCallStatus callStatus){
		List<Call> result = new ArrayList<Call>();
		for (Call call : allCall) {
			if(call.getStatus()==callStatus) {
				result.add(call);
			}
		}
		return result;
	}

		
		public Call getSelectedCall(){
			return callSelected;
		}


	public Boolean getShowDialer() {
		return showDialer;
	}



	public void setShowDialer(Boolean showDialer) {
		this.showDialer = showDialer;
	}



	public void switchValueShowDialer() {
		if (this.showDialer) this.showDialer=false;
		else this.showDialer=true;
	}

	public void resetClicked() {
		for (Call call : allCall) {
			call.setClickedOnGui(false);
		}
		
	}

	public boolean getShowSoundSetting() {
		return ShowSoundSetting;
	}
	public void switchValueShowSoundSetting() {
		if (this.ShowSoundSetting) this.ShowSoundSetting=false;
		else this.ShowSoundSetting=true;
	}
	
	public SoundSetting getSoundSetting() {
		return defaultSoundSetting;
	}

	public void setSoundSetting(SoundSetting soundSetting) {
		this.defaultSoundSetting = soundSetting;
	}

	public ConcurrentLinkedQueue<Integer> getFifo() {
		return fifo;
	}

	public Properties getProperties() {
		return properties;
	}
	
	
	
	public List<Call> getAllCall() {
		
		return this.allCall;
	}

	public Call getCallSelected() {
		return callSelected;
	}

	public void setCallSelected(Call callSelected) {
		this.callSelected = callSelected;
	}


	
}
